import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { TrackType } from './types/track';
import{ClipSettings} from '@/services/meta/types/index';


// 选中片段的接口
export interface SelectedSegmentInfo {
    id: string; // 片段ID
    start: number; // 开始时间
    duration: number; // 持续时间
    type: TrackType; // 片段类型
    trackId: string; // 轨道ID
    draftId: string; // 草稿ID
    clip: ClipSettings; 
    volume?: number; // 音量
    material_id: string; // 素材ID
}

// 选中片段的状态接口
export interface SelectedSegmentState {
    selectedSegment: SelectedSegmentInfo | null;
}


// 初始状态
const initialState: SelectedSegmentState = {
    selectedSegment: null,
};

// 创建selectedSegmentSlice
const selectedSegmentSlice = createSlice({
    name: 'selectedSegment',
    initialState,
    reducers: {
        // 设置选中的片段
        setSelectedSegment: (state, action: PayloadAction<SelectedSegmentInfo | null>) => {
            state.selectedSegment = action.payload;
        },
        // 清除选中的片段
        clearSelectedSegment: (state) => {
            state.selectedSegment = null;
        },
        // 更新选中片段的属性
        updateSelectedSegment: (state, action: PayloadAction<Partial<SelectedSegmentInfo>>) => {
            if (state.selectedSegment) {
                state.selectedSegment = {
                    ...state.selectedSegment,
                    ...action.payload,
                };
            }
        },
    },
});

// 导出actions
export const { setSelectedSegment, clearSelectedSegment, updateSelectedSegment } = selectedSegmentSlice.actions;

// 导出reducer
export default selectedSegmentSlice.reducer;

// 选择器
export const selectSelectedSegment = (state: any) => 
    state.selectedSegment.selectedSegment;